//
//  DSNotLoggedSelectController.m
//  DSJY
//
//  Created by 冯华强 on 17/3/26.
//  Copyright © 2017年 dsjy. All rights reserved.
//  未登录时的选择，游客登录、用户登录、用户注册

#import "DSNotLoggedSelectController.h"
#import "DSLoginViewModel.h"
#import "DSLoginController.h"
#import "UIStoryboard+Extension.h"
#import "UIWindow+Extension.h"
#import "MTNavigationController.h"
#import "DSRegisgerController.h"
#import "DSAccountViewModel.h"
#import "DSUserNameAndPdViewModel.h"
#import "DSAccount.h"

@interface DSNotLoggedSelectController ()
//登录VM
@property (nonatomic, strong) DSLoginViewModel *loginVM;
@end

@implementation DSNotLoggedSelectController
#pragma mark - 懒加载
- (DSLoginViewModel *)loginVM
{
    if (_loginVM == nil) {
        _loginVM = [[DSLoginViewModel alloc] init];
    }
    return _loginVM;
}

#pragma mark - 系统回调
- (void)viewDidLoad
{
    [super viewDidLoad];
    //绑定事件
    [self bindEvent];
}

#pragma mark - 设置事件
- (void)bindEvent
{
    //登录成功
    [[[MTNotificationCenter rac_addObserverForName:DSLoginRegisgerSuccessNote object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *noti) {
        //非游客，登录、注册成功后，保存用户名和pd
        DSAccount *account = [DSAccountViewModel sharedAccount].account;
        if (account && !account.isVisitors) {
            account = [DSAccountViewModel sharedAccount].isRememberUserNamePd ? account : nil;
            [DSUserNameAndPdViewModel sharedAccount].account = account;
            [[DSUserNameAndPdViewModel sharedAccount] write];
        }
        
        //切换首页控制器
        [UIWindow switchRootVCToAppDelegateWin];
    }];
}

//游客登录
- (IBAction)touristsButtonClick
{
    [self.loginVM.visitorsLoginCommand execute:nil];
}

//用户登录
- (IBAction)userLoginButtonClick
{
    DSLoginController *loginVC = (DSLoginController *)[UIStoryboard initialVCWithName:@"DSLoginController"];
    MTNavigationController *nav = [[MTNavigationController alloc] initWithRootViewController:loginVC];
    loginVC.title = @"登录";
    loginVC.showBackItem = YES;
    [self presentViewController:nav animated:YES completion:nil];
}

//用户注册
- (IBAction)userRegisterButtonClick
{
    DSRegisgerController *regisgerVC = (DSRegisgerController *)[UIStoryboard initialVCWithName:@"DSRegisgerController"];
    MTNavigationController *nav = [[MTNavigationController alloc] initWithRootViewController:regisgerVC];
    regisgerVC.title = @"注册";
    regisgerVC.showBackItem = YES;
    [self presentViewController:nav animated:YES completion:nil];
}


@end
